package com.hdc.sell.converter;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.hdc.sell.dataobject.OrderDetail;
import com.hdc.sell.dto.OrderDTO;
import com.hdc.sell.enums.ResultEnum;
import com.hdc.sell.exception.SellException;
import com.hdc.sell.form.OrderForm;
import lombok.extern.slf4j.Slf4j;
import sun.rmi.runtime.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 郭丽然
 * @program: sell
 * @date 2020/5/23 17:22
 * 功能：
 */
@Slf4j
public class OrderForm2OrderDTOConverter {
    public static OrderDTO conver(OrderForm orderForm) {
        Gson gson =new Gson();
        OrderDTO orderDTO =new OrderDTO();
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getOpenid());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());
        //items->list
        List<OrderDetail> orderDetailList=new ArrayList<>();
        try {
            orderDetailList=gson.fromJson(orderForm.getItems(),
                    new TypeToken<List<OrderDetail>>(){}.getType());
        } catch (JsonSyntaxException e) {
            log.error("[对象转换]错误，String={}",orderForm.getItems());
            throw new SellException(ResultEnum.PARAM_EEROR);
        }

        orderDTO.setOrderDetailList(orderDetailList);

        return orderDTO;
    }
}
